home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / apt-xapian-index / examples / axi-query-adaptivecutoff.py < prev    next >
Encoding:
Python Source  |  2008-04-23  |  2.6 KB  |  80 lines

  1. #!/usr/bin/python
  2.  
  3. # axi-query-adaptivecutoff - Use an adaptive cutoff to select results
  4. #
  5. # Copyright (C) 2007  Enrico Zini <enrico@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21. from optparse import OptionParser
  22. import sys
  23.  
  24. VERSION="0.1"
  25.  
  26. # Let's start with a simple command line parser with help
  27. class Parser(OptionParser):
  28.     def __init__(self, *args, **kwargs):
  29.         OptionParser.__init__(self, *args, **kwargs)
  30.  
  31.     def error(self, msg):
  32.         sys.stderr.write("%s: error: %s\n\n" % (self.get_prog_name(), msg))
  33.         self.print_help(sys.stderr)
  34.         sys.exit(2)
  35.  
  36. parser = Parser(usage="usage: %prog [options] keywords",
  37.                 version="%prog "+ VERSION,
  38.                 description="Query the Apt Xapian index.  Command line arguments can be keywords or Debtags tags")
  39. parser.add_option("-t", "--type", help="package type, one of 'game', 'gui', 'cmdline' or 'editor'")
  40.  
  41. (options, args) = parser.parse_args()
  42.  
  43.  
  44. # Import the rest here so we don't need dependencies to be installed only to
  45. # print commandline help
  46. import os
  47. import xapian
  48. from aptxapianindex import *
  49.  
  50.  
  51. # Instantiate a xapian.Database object for read only access to the index
  52. db = xapian.Database(XAPIANDB)
  53.  
  54. # Build the base query as seen in axi-query-simple.py
  55. query = xapian.Query(xapian.Query.OP_OR, termsForSimpleQuery(args))
  56.  
  57. # Add the simple user filter, if requeste
  58. query = addSimpleFilterToQuery(query, options.type)
  59.  
  60. # Perform the query
  61. enquire = xapian.Enquire(db)
  62. enquire.set_query(query)
  63.  
  64. # Retrieve the first result, and check its relevance
  65. matches = enquire.get_mset(0, 1)
  66. topWeight = matches[0].weight
  67.  
  68. # Tell Xapian that we only want results that are at least 70% as good as that
  69. enquire.set_cutoff(0, topWeight * 0.7)
  70.  
  71. # Now we have a meaningful cutoff, so we can get a larger number of results:
  72. # thanks to the cutoff, approximate results will stop before starting to be
  73. # really bad.
  74. matches = enquire.get_mset(0, 200)
  75.  
  76. # Display the results
  77. show_mset(matches)
  78.  
  79. sys.exit(0)
  80.